<?php
/*
 * @component AlphaGetCouponCode
 * @copyright Copyright (C) 2008-2009 Bernard Gilly
 * @license : GNU/GPL
 * @Website : http://www.alphaplug.com
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport( 'joomla.application.component.model' );

class alphagetcouponcodeModelalphagetcouponcode extends Jmodel {

	function __construct(){
		parent::__construct();
		
	}
	
	function _getParamsAGCC() {
	
		// Get the parameters of the active menu item
		$menus	    = &JSite::getMenu();
		$menu       = $menus->getActive();
		$menuid     = $menu->id;
		$params     = $menus->getParams($menuid);
		
		return $params;
	
	}
	
	function _getParamsAGCC2($rmenuid) {
	
		// Get the parameters of the active menu item
		$menus	    = &JSite::getMenu();
		$params     = $menus->getParams($rmenuid);
		
		return $params;
	
	}
	
	function _getRealItemid() {
		$db	=& JFactory::getDBO();
		
	    $db->setQuery( "SELECT id FROM #__menu WHERE link='index.php?option=com_alphagetcouponcode&view=getcouponcode' AND published='1'" );		
	    $realItemid = $db->loadResult();
		return $realItemid;		
	}
	
	
	
	function _checkVirtueMart() {
		if ( file_exists( JPATH_SITE.DS.'components'.DS.'com_virtuemart'.DS.'virtuemart.php') ) {
			 return true;
		} else return false;
	}	
	
	function _getCouponCodeVM( $coupontype ) {
	
		$db	=& JFactory::getDBO();
		
		$coupon_type = ( $coupontype!='both' ) ? " AND `coupon_type`='$coupontype'" : "";

		$query = "SELECT * FROM #__vm_coupons WHERE `coupon_value`>'0.00' $coupon_type";
		$db->setQuery( $query );
		$result = $db->loadObjectList();
		
		return $result;
	
	}
	
	function _getCodeCoupon( $id ) {
		$db	=& JFactory::getDBO();

		$query = "SELECT coupon_code FROM #__vm_coupons WHERE `coupon_id`='$id'";
		$db->setQuery( $query );
		$result = $db->loadResult();
		
		return $result;
	
	}
	
	function _purchase_coupon( $referrerid, $id, $points, $coupontype ) {
		global $mainframe;
	
		$db	=& JFactory::getDBO();
		
		$code_coupon = $this->_getCodeCoupon( $id );
	
		$points = (-1 * $points);
		
		$validity 	= ( $coupontype=='gift' )? JText::_( 'AGCC_USED_ONLY_ONCE' ) : JText::_( 'AGCC_PERMANENT' );
		$datas 		= JText::_( 'AGCC_CODE' ) . " " . $code_coupon . " - " . $validity;

		// AlphaUserPoints component		
		AlphaUserPointsHelper::userpoints( 'plgaup_alphagetcouponcode_vm', $referrerid, 0, $code_coupon, $datas, $points );
		
		// send notification coupon code by email
		$MailFrom	= $mainframe->getCfg('mailfrom'); 	
		$FromName	= $mainframe->getCfg('fromname'); 
		$SiteName	= $mainframe->getCfg('sitename');			
		$userinfo   = AlphaUserPointsHelper::getUserInfo( $referrerid );		
		$email	    = $userinfo->email;		
		if ( !$userinfo->block )
		{		
			$subject = JText::_('AGCC_CODE');
			$message = JText::_('AGCC_YOUR_COUPON_CODE') . ' ' . $code_coupon . " - " . $validity;
			
			JUtility::sendMail( $MailFrom, $FromName, $email, $subject, $message );
		}		
		
		return $code_coupon;
	
	}
	
	function _checkUser()  {
		global $mainframe;
		
		// active user
		$user = & JFactory::getUser();	
		
		// check referre ID
		$referrerid = @$_SESSION['referrerid'];		
		
		if ( !$user->id || !$referrerid ) {		
			$msg = JText::_('ALERTNOTAUTH' );
			$mainframe->redirect('index.php', $msg);
		} else return $referrerid;
		
	}

	function _getReferreid()  {
		global $mainframe;
		
		// check referre ID
		$referrerid = @$_SESSION['referrerid'];	
		
		return $referrerid;
		
	}
	
	function _getRuleID ( $plugin_function ) {
	
		$db	=& JFactory::getDBO();
		 
		$query = "SELECT id FROM #__alpha_userpoints_rules WHERE plugin_function='$plugin_function'";
		$db->setQuery( $query );
		$result = $db->loadResult();
		
		return $result;	
	}
	
	function _getArticleDescription ( $idArticle ){
	
		$db	=& JFactory::getDBO();
		
		$query = "SELECT id, title, introtext FROM #__content WHERE id='$idArticle'";
		$db->setQuery( $query );
		$result = $db->loadObjectList();
		
		return $result;	
		
	
	}
	
}
?>